<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="入账时间" prop="rechargeTime">
        <el-date-picker v-model="rechargeTime" type="daterange" value-format="YYYY-MM-DD" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期" placeholder="选择入账时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">


      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" :loading="exportLoading" @click="handleExport"
          v-hasPermi="['system:recharge:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rechargeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="账户金额" align="center" prop="accountAmount" />
      <el-table-column label="入账金额" align="center" prop="rechargeAmount" />
      <el-table-column label="充值方式" align="center" prop="paymentType" />
      <el-table-column label="vip变更等级" align="center" prop="vipLevel" />
      <el-table-column label="入账时间" align="center" prop="rechargeTime" width="180">
      </el-table-column>
      <el-table-column label="入账描述" align="center" prop="rechargeDesc" />
      <el-table-column label="充值类型" align="center" prop="rechargeType" />

    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改用户充值记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup name="RechargeDetail">
import { ref, reactive, toRefs, onMounted, getCurrentInstance } from 'vue'
import { useRoute } from 'vue-router'
import { listRecharge, exportRecharge } from '@/api/system/recharge'

const { proxy } = getCurrentInstance()
const route = useRoute()

// local reactive state
const loading = ref(true)
const exportLoading = ref(false)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const showSearch = ref(true)
const total = ref(0)
const rechargeList = ref([])
const title = ref('')
const open = ref(false)
const rechargeTime = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    // kept for potential route param mapping; may be null
    userId: null,
    userName: null,
    createTime: [],
    updateTime: null
  },
  rules: {}
})

const { queryParams, form } = toRefs(data)

function getList() {
  loading.value = true
  // if route contains a userId param, include it
  queryParams.value.userId = route.params.userId
  return listRecharge(proxy.addDateRange(queryParams.value, rechargeTime.value, 'RechargeTime')).then(response => {
    rechargeList.value = response.rows
    total.value = response.total
    loading.value = false
  }).catch(() => {
    loading.value = false
  })
}

function cancel() {
  open.value = false
  reset()
}

function reset() {
  data.form = {
    id: null,
    userId: null,
    userName: null,
    accountAmount: null,
    rechargeAmount: null,
    paymentType: null,
    vipLevel: null,
    rechargeDesc: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    rechargeType: null
  }
  rechargeTime.value = []
  proxy.resetForm('form')
}

function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  proxy.resetForm('queryForm')
  rechargeTime.value = []
  handleQuery()
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

function submitForm() {
  // keep same UX as before: editing/creation disabled
  proxy.$refs['form'].validate(valid => {
    if (valid) {
      if (typeof proxy.msgError === 'function') {
        proxy.msgError('编辑已被禁用，无法保存变更')
      } else {
        proxy.$message.error('编辑已被禁用，无法保存变更')
      }
    }
  })
}

function handleExport() {
  const params = queryParams.value
  proxy.$confirm('是否确认导出所有用户充值记录数据项?', '警告', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
    .then(() => {
      exportLoading.value = true
      return exportRecharge(params)
    })
    .then(response => {
      proxy.download(response.msg)
      exportLoading.value = false
    })
    .catch(() => {
      exportLoading.value = false
    })
}

onMounted(() => {
  getList()
})
</script>
